<?php

namespace addons\zzyy\library;

use app\admin\model\zzyy\ZzyySetting;
use fast\Http;
use fast\Random;
use think\Env;
use think\Log;

/**
 * 日志记录类
 */
class Yunding
{

    public function isElemeterSwitchOn($homeId, $uuid)
    {
        $config = get_addon_config('zzyy');

        $domain = $config['yunding_domain'];
        $clientId = $config['yunding_client_id'];
        $clientSecret = $config['yunding_client_secret'];

        $yundingAccesstoken = ZzyySetting::where('name', 'yunding_accesstoken')->find();
        $yundingExpirestime = ZzyySetting::where('name', 'yunding_expires_time')->find();

        $headers = ['Content-Type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        if (empty($yundingAccesstoken->value) or empty($yundingExpirestime->value)
            or $yundingExpirestime->value < time() + 3600 * 24 * 2) {

            $url = $domain . '/access_token';

            $postdata = json_encode([
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
            ]);

            $returnData = json_decode(Http::post($url, $postdata, $options),  true);

            if (0 == $returnData['ErrNo']) {
                $yundingAccesstoken->value = $returnData['access_token'];
                $yundingExpirestime->value = $returnData['expires_time'];

                $yundingAccesstoken->save();
                $yundingExpirestime->save();
            } else {
                Log::record($returnData, 'yunding_elemeter');
                return false;
            }
        }

        $url = $domain . '/get_elemeter_info';
        $url = $url . '?access_token=' . $yundingAccesstoken->value . '&home_id=' . $homeId . '&uuid=' . $uuid;

        $returnData = json_decode(Http::get($url, [], $options), true);

        if (0 == $returnData['ErrNo']) {
            if (isset($returnData['enable_state']) and 1 == $returnData['enable_state']) {
                return true;
            } else {
                Log::record($returnData, 'yunding_elemeter');
                return false;
            }
        } else {
            Log::record($returnData, 'yunding_elemeter');
            return false;
        }
    }

    public function elemeterSwitchOn($homeId, $uuid)
    {
        $config = get_addon_config('zzyy');

        $domain = $config['yunding_domain'];
        $clientId = $config['yunding_client_id'];
        $clientSecret = $config['yunding_client_secret'];

        $yundingAccesstoken = ZzyySetting::where('name', 'yunding_accesstoken')->find();
        $yundingExpirestime = ZzyySetting::where('name', 'yunding_expires_time')->find();

        $headers = ['Content-Type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        if (empty($yundingAccesstoken->value) or empty($yundingExpirestime->value)
            or $yundingExpirestime->value < time() + 3600 * 24 * 2) {

            $url = $domain . '/access_token';

            $postdata = json_encode([
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
            ]);

            $returnData = json_decode(Http::post($url, $postdata, $options), true);
            if (0 == $returnData['ErrNo']) {
                $yundingAccesstoken->value = $returnData['access_token'];
                $yundingExpirestime->value = $returnData['expires_time'];

                $yundingAccesstoken->save();
                $yundingExpirestime->save();
            } else {
                Log::record($returnData, 'yunding_elemeter');
                return false;
            }
        }

        $url = $domain . '/elemeter_switch_on';

        $postdata = json_encode([
            'access_token' => $yundingAccesstoken->value,
            'home_id' => $homeId,
            'uuid' => $uuid,
        ]);


        $returnData = json_decode(Http::post($url, $postdata, $options), true);

        if (0 == $returnData['ErrNo']) {
            return true;
        } else {
            Log::record($returnData, 'yunding_elemeter');
            return false;
        }

    }

    public function elemeterSwitchOff($homeId, $uuid)
    {
        $config = get_addon_config('zzyy');

        $domain = $config['yunding_domain'];
        $clientId = $config['yunding_client_id'];
        $clientSecret = $config['yunding_client_secret'];

        $yundingAccesstoken = ZzyySetting::where('name', 'yunding_accesstoken')->find();
        $yundingExpirestime = ZzyySetting::where('name', 'yunding_expires_time')->find();

        $headers = ['Content-Type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        if (empty($yundingAccesstoken->value) or empty($yundingExpirestime->value)
            or $yundingExpirestime->value < time() + 3600 * 24 * 2) {

            $url = $domain . '/access_token';

            $postdata = json_encode([
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
            ]);

            $returnData = json_decode(Http::post($url, $postdata, $options), true);
            if (0 == $returnData['ErrNo']) {
                $yundingAccesstoken->value = $returnData['access_token'];
                $yundingExpirestime->value = $returnData['expires_time'];

                $yundingAccesstoken->save();
                $yundingExpirestime->save();
            } else {
                Log::record($returnData, 'yunding_elemeter');
                return false;
            }
        }

        $url = $domain . '/elemeter_switch_off';

        $postdata = json_encode([
            'access_token' => $yundingAccesstoken->value,
            'home_id' => $homeId,
            'uuid' => $uuid,
        ]);


        $returnData = json_decode(Http::post($url, $postdata, $options), true);

        if (0 == $returnData['ErrNo']) {
            return true;
        } else {
            Log::record($returnData, 'yunding_elemeter');
            return false;
        }

    }
}
